package org.example.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Configuration
public class I18NAutoConfiguration {

    @Autowired(required = false)
    private List<I18NPropertiesLoader> loaders = Collections.emptyList();

    @Bean
    public MessageSource messageSource() {
//        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
//        // 打印所有加载的路径（调试用）
//        System.out.println("Loading i18n paths: " + loaders.stream()
//                .map(loader -> "classpath:" + loader.path()) // 添加前缀
//                .collect(Collectors.toList()));
//        messageSource.setBasenames(loaders.stream()
//                .map(I18NPropertiesLoader::path)
//                .toArray(String[]::new));
//        messageSource.setDefaultEncoding("UTF-8");
//        return messageSource;
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames("classpath:i18n/coupon"); // 硬编码路径
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }
}
